{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "93ff5627",
   "metadata": {},
   "source": [
    "### 2.  判断短字符串b的所有字符是否都在长字符串a中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c84ca0bf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "//轮询 O(mn)\n",
    "#include <string>\n",
    "#include <iostream>\n",
    "bool StringContain(const std::string &a, const std::string &b){\n",
    "    for(int i = 0; i < b.length(); ++i){\n",
    "        int j = 0;\n",
    "        for(j = 0; (j < a.length()) && (a[j] != b[i]);j++){}\n",
    "        if(j >= a.length()) return false;\n",
    "    }\n",
    "    return true;\n",
    "}\n",
    "std::string a = \"123456789609dffg\";\n",
    "std::string b = \"3425\";\n",
    "std::string c = \"xiaoyang\";\n",
    "std::cout << (StringContain(a,b) ? \"True\":\"False\") << std::endl;\n",
    "std::cout << (StringContain(a,c) ? \"True\":\"False\") << std::endl;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7be8bfb1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "// 先排序后轮询\n",
    "#include <algorithm>\n",
    "#include <string>\n",
    "#include <iostream>\n",
    "bool StringContain(std::string &a, std::string &b){\n",
    "    std::sort(a.begin(), a.end());\n",
    "    std::sort(b.begin(), b.end());\n",
    "    for(int pa = 0, pb = 0; pb < b.length();) {\n",
    "        while((pa < a.length()) && (a[pa] < b[pb])) pa++;\n",
    "        if((pa < a.length()) && (a[pa] > b[pb])) return false;\n",
    "        ++pb;\n",
    "    }\n",
    "    return true;\n",
    "}\n",
    "std::string a = \"123456789609dffg\";\n",
    "std::string b = \"3425\";\n",
    "std::string c = \"xiaoyang\";\n",
    "std::cout << (StringContain(a,b) ? \"True\":\"False\") << std::endl;\n",
    "std::cout << (StringContain(a,c) ? \"True\":\"False\") << std::endl;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0c4853a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "// hash\n",
    "#include <string>\n",
    "#include <iostream>\n",
    "bool StringContain(std::string &a, std::string &b){\n",
    "    int p[128] = {0};\n",
    "    for(auto char_b : b) p[char_b - 'A'] = 1;\n",
    "    for(auto char_a : a) p[char_a - 'A'] = 0;\n",
    "    for(int i = 0; i < 128; i++){\n",
    "        if(p[i] == 1) return false;\n",
    "    }\n",
    "    return true;\n",
    "}\n",
    "std::string a = \"123456789609dffg\";\n",
    "std::string b = \"3425\";\n",
    "std::string c = \"xiaoyang\";\n",
    "std::cout << (StringContain(a,b) ? \"True\":\"False\") << std::endl;\n",
    "std::cout << (StringContain(a,c) ? \"True\":\"False\") << std::endl;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "34458920",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "// bit\n",
    "#include <string>\n",
    "#include <iostream>\n",
    "bool StringContain(std::string &a, std::string &b){\n",
    "    int hash = 0;\n",
    "    for(auto char_a : a) hash |= (1 << (char_a - 'A'));\n",
    "    for(auto char_b : b){\n",
    "        if((hash & (1 << (char_b - 'A'))) == 0){\n",
    "            return false;\n",
    "        }\n",
    "    }\n",
    "    return true;\n",
    "}\n",
    "std::string a = \"123456789609dffg\";\n",
    "std::string b = \"3425\";\n",
    "std::string c = \"xiaoyang\";\n",
    "std::cout << (StringContain(a,b) ? \"True\":\"False\") << std::endl;\n",
    "std::cout << (StringContain(a,c) ? \"True\":\"False\") << std::endl;\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++14",
   "language": "C++14",
   "name": "xcpp14"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
